package com.obrempon.credit.market.ui.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonDialog
import com.obrempon.credit.market.databinding.ObremponPhotoDialogBinding
import com.obrempon.credit.market.event.ObremponEventPointType

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/22 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class ObremponPhotoDialog: CommonDialog {
    private lateinit var binding: ObremponPhotoDialogBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //默认选择拍照
    private var selecetedType = 1

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置备注
     */
    fun setEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeObremponRootView(): View {
        binding = ObremponPhotoDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initObremponView() {
        selecetedType = 1
        checkObremponSelectType()
    }

    override fun initObremponListener() {
        setOnClickListener(binding.imgPhotoClose)//关闭
        setOnClickListener(binding.cameraLayout)//拍照
        setOnClickListener(binding.albumLayout)//相册选择
        setOnClickListener(binding.buttonConfirm)//确认
    }

    override fun takeObremponData() {

    }

    override fun onObremponViewClick(v: View?) {
        when(v!!.id){
            R.id.cameraLayout -> {//拍照
                selecetedType = 1
                checkObremponSelectType()
            }
            R.id.albumLayout -> {//相册选择
                selecetedType = 2
                checkObremponSelectType()
            }
            R.id.buttonConfirm -> {//确定
                if (selecetedType == 1){
                    showPhotoCameraClick()
                } else {
                    showPhotoAlbumClick()
                }
                dismiss()
            }
            else -> {
                dismiss()
            }
        }
    }

    /**
     * 切换拍照、相册选择
     */
    private fun checkObremponSelectType(){
        binding.cameraLayout.shapeDrawableBuilder
            .setSolidColor(getColor(R.color.color_fff5f5f5))
            .setStrokeColor(getColor(R.color.color_fff5f5f5))
            .intoBackground()
        binding.imgCamera.setImageResource(R.mipmap.obrempon_photo_camera_normal)
        binding.textCamera.setTextColor(getColor(R.color.color_ffb8b8b8))
        binding.imgCameraSelected.visibility = View.INVISIBLE

        binding.albumLayout.shapeDrawableBuilder
            .setSolidColor(getColor(R.color.color_fff5f5f5))
            .setStrokeColor(getColor(R.color.color_fff5f5f5))
            .intoBackground()
        binding.imgAlbum.setImageResource(R.mipmap.obrempon_photo_album_normal)
        binding.textAlbum.setTextColor(getColor(R.color.color_ffb8b8b8))
        binding.imgAlbumSelected.visibility = View.INVISIBLE

        when(selecetedType){
            1 -> {
                binding.cameraLayout.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.color_fff9fcff))
                    .setStrokeColor(getColor(R.color.color_ff0569ff))
                    .intoBackground()
                binding.imgCamera.setImageResource(R.mipmap.obrempon_photo_camera_selected)
                binding.textCamera.setTextColor(getColor(R.color.color_ff0569ff))
                binding.imgCameraSelected.visibility = View.VISIBLE
            }
            else -> {
                binding.albumLayout.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.color_fff9fcff))
                    .setStrokeColor(getColor(R.color.color_ff0569ff))
                    .intoBackground()
                binding.imgAlbum.setImageResource(R.mipmap.obrempon_photo_album_selected)
                binding.textAlbum.setTextColor(getColor(R.color.color_ff0569ff))
                binding.imgAlbumSelected.visibility = View.VISIBLE
            }
        }
    }

    /**
     * 拍照
     */
    private fun showPhotoCameraClick(){
        if(mCallback != null){
            mCallback?.openCamera()
        }
//        val permissions = arrayListOf(
//            Manifest.permission.CAMERA
//        )
//        if (XXPermissions.isGranted(context,permissions)){
//            if(mCallback != null){
//                dismiss()
//                mCallback!!.openCamera()
//            }
//        } else {
//            XXPermissions.with(context)
//                // 申请多个权限
//                .permission(permissions)
//                .request(object: OnPermissionCallback {
//                    override fun onGranted(p0: MutableList<String>, p1: Boolean) {
//                        var permissionString = ""
//                        p0.forEach {
//                            permissionString = "$permissionString$it,"
//                        }
//                        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
//                        if(mCallback != null){
//                            dismiss()
//                            mCallback!!.openCamera()
//                        }
//                    }
//                    override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
//                        var permissionString = ""
//                        p0.forEach {
//                            permissionString = "$permissionString$it,"
//                        }
//                        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
//                        XXPermissions.startPermissionActivity(context,permissions)
//                    }
//                })
//        }
    }

    /**
     * 相册
     */
    private fun showPhotoAlbumClick(){
        if(mCallback != null){
            mCallback?.openAlbum()
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}